package com.iot.ic.controller;

import com.iot.ic.base.BaseController;
import com.iot.ic.common.Options;
import com.iot.ic.common.Response;
import com.iot.ic.common.ReturnCode;
import com.iot.ic.model.MatchScenesInfo;
import com.iot.ic.model.StadiumInfo;
import com.iot.ic.model.StadiumScenes;
import com.iot.ic.model.TeamInfo;
import com.iot.ic.service.MatchScenesService;
import com.iot.ic.service.StadiumScenesService;
import com.iot.ic.service.StadiumService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;

@Api(description = "赛道信息相关接口")
@RestController
public class StadiumController extends BaseController {
	
	@Autowired
	private StadiumService stadiumService;
	
	@Autowired
	private StadiumScenesService stadiumScenesService;
	
	@Autowired
	private MatchScenesService matchScenesService;
	
	/**
	 * 获得赛道信息列表
	 * @param RefereeInfo
	 * @return
	 */
	@ApiOperation(value="获得赛道信息列表", notes="")
	@ResponseBody
	@RequestMapping(value="/users/{userId}/stadiums", method=RequestMethod.GET)
	public Response listStadiumInfos(@PathVariable("userId") int userId,
			@RequestParam(value="currentPage") int currentPage,
			@RequestParam(value="pageSize") int pageSize){
		Response response = new Response();
		StadiumInfo stadiumInfo = new StadiumInfo();
		stadiumInfo.setCurrentPage(currentPage);
		stadiumInfo.setLength(pageSize);
		List<StadiumInfo> stadiumInfoList = stadiumService.listStadiumInfos(stadiumInfo);
		response.setResult(stadiumInfoList);
		return response;
	}
	
    @ApiOperation(value = "增加赛道信息", notes = "")
    @PostMapping(value = "/users/{userId}/stadiums")
    public Response addStadiumInfo(@PathVariable(value = "userId") Integer userId, @RequestBody @Valid StadiumInfo stadiumInfo) {
        Response response = new Response();
        stadiumInfo.setCreateTime(new Date());
        stadiumInfo.setDelFlag(Options.NotDel.getCode());
        int result=stadiumService.addStadiumInfos(stadiumInfo);
        if (result==1)
            response.setInfos(ReturnCode.No_AddStadiumOK);
        else
            response.setInfos(ReturnCode.Err_AddStadiumFailor);
        return  response;
    }
    
    
    @ApiOperation(value = "修改赛道信息", notes = "")
    @ResponseBody
	@RequestMapping(value="/users/{userId}/stadiums/{stadiumId}", method=RequestMethod.PUT)
    public Response updateStadiumInfo(@PathVariable(value = "userId") Integer userId, 
    		@PathVariable(value = "stadiumId") Integer stadiumId,
    		@RequestBody @Valid StadiumInfo stadiumInfo) {
        Response response = new Response();
        stadiumInfo.setStadiumId(stadiumId);
        int result=stadiumService.updateStadiumInfo(stadiumInfo);
        if (result==1)
            response.setInfos(ReturnCode.No_UpdateStadiumOK);
        else
            response.setInfos(ReturnCode.Err_UpdateStadiumFailor);
        return  response;
    }
    
    @ApiOperation(value = "删除赛道信息", notes = "")
    @DeleteMapping(value = "/users/{userId}/stadiums/{stadiumId}")
    public Response deleteStadiumInfo(@PathVariable(value = "userId") Integer userId, 
    		@PathVariable(value = "stadiumId") Integer stadiumId) {
        Response response = new Response();
        int result=stadiumService.deleteStadiumInfo(stadiumId);
        if (result==1)
            response.setInfos(ReturnCode.No_DeleteStadiumOK);
        else
            response.setInfos(ReturnCode.No_DeleteStadiumOK);
        return  response;
    }
    
    @ApiOperation(value = "获得赛道详细信息", notes = "")
    @RequestMapping(value="/users/{userId}/stadiums/{stadiumId}", method=RequestMethod.GET)
    public Response getStadiumInfo(@PathVariable(value = "userId") Integer userId, 
    		@PathVariable(value = "stadiumId") Integer stadiumId) {
        Response response = new Response();
        StadiumInfo result=stadiumService.getStadiumInfoWithSceneList(stadiumId);
        response.setResult(result);
        return response;
    }
    
    /**
	 * 修改用户绑定设备
	 * @param user
	 * @param userId
	 * @return
	 */
	@ApiOperation(value="修改赛道与场景绑定", notes="")
	@ResponseBody
    @RequestMapping(value="/stadium/{stadiumId}/scenes",method=RequestMethod.POST)
	public Response updateUserDevie( @RequestParam(value="scenesIds", required=false) String scenesIds,@PathVariable int stadiumId){
        
		Response response =new Response();
		int flag = stadiumScenesService.bindStadiumScenes(scenesIds, stadiumId);
		if (flag > 0) {
    		response.setInfos(ReturnCode.No_Err);
    	}else {
    		response.setInfos(ReturnCode.Err_RequestFailed);
    	}
		return response;
	}
	
	
	/**
	 * 获得赛道与场景绑定的场景列表
	 * @param user
	 * @param userId
	 * @return
	 */
	@ApiOperation(value="获得赛道与场景绑定的场景列表", notes="")
	@ResponseBody
    @RequestMapping(value="/users/{userId}/stadium/{stadiumId}/stadiumScenes",method=RequestMethod.GET)
	public Response listStadiumScenesByStadium(@PathVariable(value = "userId") Integer userId,
			@PathVariable(value = "stadiumId") Integer stadiumId){      
		Response response =new Response();
		List<MatchScenesInfo> scenesList = matchScenesService.listBoundScenesByStadium(stadiumId);
        response.setResult(scenesList);
		return response;
	}
	

	/**
	 * 获得赛场经纬度信息
	 * @param user
	 * @param stadiumId
	 * @return
	 */
	@ApiOperation(value="获得赛场经纬度信息", notes="")
	@ResponseBody
    @RequestMapping(value="/users/{userId}/stadium/{stadiumId}/postion",method=RequestMethod.GET)
	public Response geyStadiumPosition(@PathVariable(value = "userId") Integer userId,
			@PathVariable(value = "stadiumId") Integer stadiumId){      
		Response response = new Response();
		StadiumInfo stadiumInfo = stadiumService.getStadiumPosition(stadiumId);
        response.setResult(stadiumInfo);
		return response;
	}
	
	
}
